package com.sdp.right.utils;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.extension.service.IService;



@Component
public class RunnerUtil implements ApplicationRunner {
	
	@Autowired
	private ApplicationContext applicationContext;
	
	private Map<String,Object> serviceMap = new HashMap<>();

	@Override
	public void run(ApplicationArguments args) throws Exception {
		String[] iServices = applicationContext.getBeanNamesForType(IService.class);
		
		Object service = null;
		for(String serviceName : iServices) {
			if(serviceName.indexOf("ServiceImpl")>=0) {
				service = applicationContext.getBean(serviceName);
//				System.out.println(StringUtils.capitalize(serviceName.replaceAll("ServiceImpl", "")));
//				System.out.println(applicationContext.getBean(serviceName).getClass().getSimpleName().replaceAll("ServiceImpl", ""));
				serviceMap.put(StringUtils.capitalize(serviceName.replaceAll("ServiceImpl", "")), service);
			}
			
		}
	}
	
	public Map<String,Object> getServiceMap(){
		return serviceMap;
	}

}
